package com.example.myapplication.flow

import android.os.Bundle
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.example.myapplication.R
import com.example.myapplication.databinding.FlowActivityBinding
import com.example.myapplication.test1
import com.orhanobut.logger.Logger
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

/**
 * 类描述:
 * 创建人:   tangchao$
 * 创建时间: 2021/8/25$ 8:55 AM$
 * 联系方式: 419704299@qq.com
 * 修改人:   tangchao$
 * 修改时间: 2021/8/25$ 8:55 AM$
 * 修改备注:  [说明本次修改内容]
 */
@InternalCoroutinesApi
class FlowActivity1 : AppCompatActivity(), CoroutineScope by MainScope() {
    lateinit var binding: FlowActivityBinding
    private val flowviewmodel: FlowViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView<FlowActivityBinding>(this, R.layout.flow_activity)
//        Flowtest1()
//        Flowtest2()
        testchannelFlow()

//        flowviewmodel.login()
        oberverlivedata()
    }

    private fun Flowtest2() {
        val testflow = flowOf(1, 2, 3)

        flow<Int> {
            emit(1)
            emit(2)
            emit(3)
            flowOf(6, 7, 8)
        }
//        var list = listOf<Int>(2, 3, 4).asFlow()
        var list = mutableListOf<Int>(2, 3, 4).asFlow()





        launch {
//            testflow.collect {
//                Logger.d("FlowActivity1===Flowtest2: " + it);
//                binding.textView9.text = binding.textView9.text.toString() + it
//
//            }
            list.collect {
                binding.textView9.text = binding.textView9.text.toString() + it

            }
//            testflow.collectIndexed { value ->
//
//            }

        }
    }

    fun flowFrom() = callbackFlow<Any> {
//    var callback=object :Callback

    }

    val channelflow = channelFlow<String> {
        send("hello")
        withContext(Dispatchers.IO) {
            send("channel flow")
        }
    }

    fun testchannelFlow() {
        launch {
            signEvent.collect {
                binding.textView9.text = binding.textView9.text.toString() + it

            }
        }


        var array = arrayListOf(9)
        launch {
            flow {
                emit(1)
                emit(2)
            }.toCollection(array)
        }
        array.forEach {
            Logger.d("===收集后将结果添加到集合: " + it);
            binding.textView9.text = binding.textView9.text.toString() + it

        }

        launch {
            flow {
                emit(1)
                emit(2)
            }
//                    .toList()
//                    .launchIn()
                    .toSet()
                    .forEach {
                        Logger.d("FlowActivity1===testchannelFlow:转换为集合 ");

                    }


        }
        val myflow = flow {
            emit(1)
            emit(null)
        }
        launch {
            Logger.d("===: " + myflow.last());
            Logger.d("===: " + myflow.lastOrNull());
            Logger.d("===: " + myflow.count());

        }


    }

    private fun oberverlivedata() {
        flowviewmodel.uilivedata.observe(this, Observer {
//            Toast.makeText(this, "it=" + it, Toast.LENGTH_SHORT).show()
        })
    }

    val uiState = MutableStateFlow("1")
    val signEvent = MutableSharedFlow<String>()

    private fun Flowtest1() {

        val testFlow = flow<String> {
            emit("hello")
            emit("FLOW20210826")
        }
        Logger.d("FlowActivity1===Flowtest1: ");

        var job = Job()
        var coroutineScope = CoroutineScope(Dispatchers.Main + job)
        coroutineScope.launch {
            testFlow.collect(object : FlowCollector<String> {
                override suspend fun emit(value: String) {
                    Logger.d("FlowActivity1===emit: " + value);
//                    binding.textView9.text = value
                    uiState.value = value
                    binding.textView9.text = uiState.value


                }
            })

            signEvent.collect(object : FlowCollector<String> {
                override suspend fun emit(value: String) {
                    Logger.d("FlowActivity1===emit: value=" + value);

                }
            })


        }
//        uiState.value="2"
        signEvent.tryEmit("helloMutableSharedFlow")
    }

}